using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace SmartRename
{
    public class RenameFile
    {
        private string _filePath;
        public string FilePath
        {
            get { return _filePath; }
            set { _filePath = value; }
        }

        public string FullPath
        {
            get
            {
                return Path.Combine(this._filePath, this._originalName);
            }
        }

        private bool _rename = true;
        public bool Rename
        {
            get { return _rename; }
            set { _rename = value; }
        }

        private string _originalName;
        public string OriginalName
        {
            get { return _originalName; }
            set { _originalName = value; }
        }

        public string OriginalNameWithoutExtension
        {
            get
            {
                return Path.GetFileNameWithoutExtension(this.OriginalName);
            }
        }
            

        private string _newName;
        public string NewName
        {
            get { return _newName; }
            set { _newName = value; }
        }

        private string _renameResult;
        public string RenameResult
        {
            get { return _renameResult; }
            set { _renameResult = value; }
        }
 

        public RenameFile(string filePath)
        {
            this._filePath = Path.GetDirectoryName(filePath);
            this._originalName = Path.GetFileName(filePath);
        }

        public void TryRename()
        {
            if (this.NewName != this.OriginalName)
            {
                //validate:
                List<char> illegals = new List<char>();
                foreach (char illegal in Path.GetInvalidFileNameChars())
                {
                    if (this._newName.IndexOf(illegal) > 0)
                    {
                        illegals.Add(illegal);
                    }
                }
                if (illegals.Count == 0)
                {
                    try
                    {
                        File.Move(Path.Combine(this._filePath, this.OriginalName), Path.Combine(this._filePath, this.NewName));
                        this.RenameResult = "Renamed";
                        this.Rename = false;
                    }
                    catch (Exception ex)
                    {
                        this.RenameResult = string.Format("Errored: {0}", ex.Message);
                    }
                }
                else
                {
                    StringBuilder resultBuilder = new StringBuilder();
                    resultBuilder.Append("Not renamed: new name contains illegal characters: ");
                    foreach (char illegal in illegals)
                    {
                        resultBuilder.Append(illegal);
                        resultBuilder.Append(", ");
                    }
                    this.RenameResult = resultBuilder.ToString().TrimEnd(", ".ToCharArray());
                }
            }
            else
            {
                this.RenameResult = "Name unchanged";
            }
        }
    }
}
